package com.five.community.service.impl;

import com.five.community.bean.Admin;
import com.five.community.mapper.AdminMapper;
import com.five.community.service.AdminService;

import com.five.community.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public List<Map<String, Object>> selectAllAdminAndRoles() throws Exception {
        return adminMapper.selectAllAdminAndRoles();
    }

    @Override
    public void insert(Admin admin) throws Exception {
        adminMapper.insert(admin);
    }

    @Override
    public Admin login(String username, String password) throws Exception {
        // 登录校验
        // ① 根据用户名查询用户数据
        Admin admin = adminMapper.selectByUsername(username);
        // ② 根据数据的密文密码和明文密码匹配
        if(admin != null) {
            String mPassword = SecurityUtils.getMD5Password(password, admin.getSalt());
            if(mPassword.equals(admin.getPassword())) {
                return admin;
            }
        }

        return null;
    }
}